import csv
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import pymysql
import openpyxl

from config.config import DRIVER_PATH


class DB:
    def __init__(self,host=None,user=None,password=None,database=None):
        self.host = host if host else "localhost"
        self.user = user if user else "root"
        self.password = password if password else ""
        self.database = database if database else "crm_login"
        self.config={
            "host":self.host,
            "user":self.user,
            "password":self.password,
            "database":self.database
        }
        self.db=pymysql.connect(**self.config)
        self.cursor=self.db.cursor()
    def qurery(self,sql):
        self.cursor.execute(sql)
        return self.cursor.fetchall()
    # def __del__(self):
    #     self.cursor.close()
    #     self.db.close()
def open_browser():
    service = Service(DRIVER_PATH+r"\chromedriver.exe")
    driver = webdriver.Chrome(service=service)
    driver.maximize_window()
    return driver
def read_excel(file=r"D:\my_unittest_demo\datas\login_datas.xlsx",sheet_name="user_data"):
    "打开excel文件"
    wb = openpyxl.load_workbook(file)
    "获得指定的sheet的行"
    ws=wb[sheet_name]
    datas=[]
    "遍历每行每列得到单元格cell"
    for w in ws:
        xx=[]
        for cell in w:
            xx.append(cell.value)
        datas.append(xx)
    return datas[1:]
def read_txt(file=r"D:\my_unittest_demo\datas\data.txt"):
    with open(file,encoding='utf-8') as f:
        next(f)  # 跳过第一行
        result = [line.strip().split() for line in f if line.strip()]#跳过空行，把每一行按空白字符切分成列表
    return result
def read_csv(filename=r"D:\my_unittest_demo\datas\login_datas.csv"):
    with open(filename, 'r', encoding='utf-8') as f:
        datas = csv.reader(f)
        next(datas)  # 使用next()跳过第一行表头
        data_list = [row for row in datas]  # 读取剩下的数据
    return data_list
if __name__ == '__main__':
  print(read_csv())